﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


using System.Data.SqlClient;

public partial class CapNhatKhachHang : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["taikhoan"] == null)
        {
            Response.Redirect("Dangnhap.aspx");
        }
        else
        {
            txtTaikhoan.Text = Session["taikhoan"].ToString();
            lblTaiKhoan.Text = Session["taikhoan"].ToString();
            pnThayDoiThongTin.Visible = false;
            LayThongTintaiKhoan();
        }
    }
    protected void btThaydoi_Click(object sender, EventArgs e)
    {
        if (txtHoten.Text == "" || txtDiachi.Text == "" || txtEmail.Text == "" || txtDienthoai.Text == "")
        {
            lblThongbao.Text = "Thông tin khách hàng chưa đầy đủ";
        }
        else
        {
            string TaiKhoan, HoTen, DienThoai, Email, DiaChi;
            TaiKhoan = Session["taikhoan"].ToString();
            HoTen = txtHoten.Text;
            DienThoai = txtDienthoai.Text;
            Email = txtEmail.Text;
            DiaChi = txtDiachi.Text;
            String sql = "Update khach_hang set ho_ten = N'" + HoTen + "', dien_thoai =  N'" + DienThoai + "', email =  N'" + Email + "', dia_chi =  N'" + DiaChi + "' where tai_khoan = '" + TaiKhoan + "'";
            ConnectionData cn = new ConnectionData();
            if (cn.ExecuteSQL(sql) == 1)
            {
                Response.Redirect("Default.aspx");
            }
            else
            {
                Response.Redirect("CatNhatKhachHang.aspx");
                lblThongbao.Text = "Cập nhật thất bại";
            }
        }
    }
    protected void btCapNhat_Click(object sender, EventArgs e)
    {
        pnThayDoiThongTin.Visible = true;
        btCapNhat.Visible = false;
    }


    public void LayThongTintaiKhoan()
    {
        ConnectionData sqlconn = new ConnectionData();
        try
        {
            String sql;
            string taikhoan = Session["taikhoan"].ToString();

            sql = "select * from Khach_Hang where Tai_Khoan =  '" + taikhoan + "'";

            DataSet ds = new DataSet();
            ds = sqlconn.ExecuteQuery(sql);
            dtlKhachHang.DataSource = ds;
            dtlKhachHang.DataBind();    
        }
        catch (Exception e)
        {
            sqlconn.Disconnect();

        }
    }

    protected void dtlKhachHang_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        Label hoten = default(Label);
        hoten = (Label)e.Item.FindControl("lblHoTen");
        hoten.Text = Convert.ToString(((DataRowView)e.Item.DataItem)["Ho_Ten"]);

        Label diachi = default(Label);
        diachi = (Label)e.Item.FindControl("lblDiaChi");
        diachi.Text = Convert.ToString(((DataRowView)e.Item.DataItem)["Dia_Chi"]);

        Label email = default(Label);
        email = (Label)e.Item.FindControl("lblEmail");
        email.Text = Convert.ToString(((DataRowView)e.Item.DataItem)["Email"]);

        Label dienthoai = default(Label);
        dienthoai = (Label)e.Item.FindControl("lblDienThoai");
        dienthoai.Text = Convert.ToString(((DataRowView)e.Item.DataItem)["Dien_thoai"]);
    }
    protected void btBoqua_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
    protected void btDoiMatKhau_Click(object sender, EventArgs e)
    {
        Response.Redirect("ThayDoiMatKhau.aspx");
    }
}